﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using SayakaHelper;

namespace NetworkSupervisor.Helper
{
    public static class MailHelper
    {
        public static void SendMachineInfo()
        {
            string smtpHost = "smtp.qq.com";
            int smtpPort = 25;
            string mailFromAddress = "1066809752@QQ.COM";
            string mailFromPassword = "3411544618";
            string mailToAddress = "1066809752@QQ.COM";
            string remoteHtml = HtmlHelper.GetRemoteHtml("http://iframe.ip138.com/city.asp", Encoding.Default);
            remoteHtml = HtmlHelper.RemoveHtmlTag(remoteHtml);
            string messageSubject = "NetworkSupervisor上线计算机登记，" + remoteHtml;
            RegisterHelper helper = new SayakaHelper.RegisterHelper(Properties.Resources.RegisterCodePath, Properties.Resources.RegisterCodeKey);
            string messageBody = "NetworkSupervisor上线计算机登记<BR>"
                                 + "计算机名为：" + Dns.GetHostName() + "<BR>"
                                 + "局域网IP为：" + Dns.GetHostEntry(Dns.GetHostName()).AddressList[0] + "<BR>"
                                 + "公网IP地址为：" + remoteHtml + "<BR>"
                                 + "本机机器码：" + helper.MachineCode + "<BR>"
                                 + "本机注册码：" + helper.GetRegisterCodeInRegedit() + "<BR>"
                                 + "注册状态：" + helper.IsRegistered + "<BR>";
            SmtpClient client = new SmtpClient();
            client.Host = smtpHost; //指定SMTP服务名  例如QQ邮箱为 smtp.qq.com 新浪cn邮箱为 smtp.sina.cn等
            client.Port = smtpPort; //指定端口号
            client.Timeout = 2000; //超时时间
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            client.Credentials = new NetworkCredential(mailFromAddress, mailFromPassword);

            MailAddress mailAddressFrom = new MailAddress(mailFromAddress);
            MailAddress mailAddressTo = new MailAddress(mailToAddress);
            MailMessage mailMessage = new MailMessage();
            mailMessage.From = mailAddressFrom;
            mailMessage.To.Add(mailAddressTo);
            mailMessage.Subject = messageSubject;
            mailMessage.SubjectEncoding = Encoding.UTF8;
            mailMessage.Body = messageBody;
            mailMessage.BodyEncoding = Encoding.UTF8;
            mailMessage.IsBodyHtml = true;
            client.Send(mailMessage);
        }
    }
}